#include "Alice/Resource/Texture2D.h"

namespace Alice
{

Texture2D::Texture2D()
    : width(0), height(0),
      internal_format(GL_RGB),
      image_format(GL_RGB),
      wrap_S(GL_REPEAT), wrap_T(GL_REPEAT),
      filter_min(GL_LINEAR), filter_max(GL_LINEAR)
{
    glGenTextures(1, &Id);
}

void Texture2D::Generate(unsigned int width, unsigned int height, unsigned char* image_data)
{
    this->width = width;
    this->height = height;
    // 绑定当前纹理
    glBindTexture(GL_TEXTURE_2D, Id);
    // 绑定材质的参数
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap_S);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap_T);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter_min);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter_max);
    // 使用图片生成纹理
    glTexImage2D(
        GL_TEXTURE_2D, 0, 
        internal_format, width, height, 0, 
        image_format, GL_UNSIGNED_BYTE,
        image_data
    );
    glGenerateMipmap(GL_TEXTURE_2D);
    // 解绑材质
    glBindTexture(GL_TEXTURE_2D, 0);
}

void Texture2D::Bind() const 
{
    glBindTexture(GL_TEXTURE_2D, Id);
}

} // namespace Alice
